package com.yanqu.road.server.cmd.activity.hell;

import com.yanqu.road.entity.activity.hell.CutLawResult;
import com.yanqu.road.entity.activity.hell.HellNpc;
import com.yanqu.road.pb.activity.HellProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.hell.HellModule;
import com.yanqu.road.server.manger.VipMgr;
import com.yanqu.road.server.manger.activity.hell.HellMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;

@Cmd(code = Protocol.S_HELL_CUT_LAW, desc = "断案")
public class HellCutLawCmd extends AbstractCommand {

    int activityId;
    int type;
    CutLawResult result;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        HellProto.HellCutLawReqMsg reqMsg = HellProto.HellCutLawReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(HellMgr.getShowTimeActivityId() == 0 || HellMgr.getShowTimeActivityId() != activityId){
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!HellMgr.activityInTime()){
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }
        if(type != 1 && type != 2){
            ret = GameErrorCode.E_HELL_CUT_LAW_PARAM_ERR;
            return;
        }
        if(type == 2 && (VipMgr.getVipLv(player.getUserInfo().getVipExp()) < HellMgr.getOneKeyVipLimit() &&
                player.getModule(HellModule.class).getUserData().getIntegral() < HellMgr.getOneKeyScoreLimit())){
            ret = GameErrorCode.E_HELL_CUT_LAW_LIMIT;
            return;
        }
        result = player.getModule(HellModule.class).cutLaw(type);
        if(result.getRet() != 0){
            ret = result.getRet();
            return;
        }

    }

    @Override
    public void sendData(GamePlayer player) {
        HellProto.HellCutLawRespMsg.Builder respMsg = HellProto.HellCutLawRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            respMsg.setIntegral(result.getIntegral());
            for(HellNpc npc : result.getNpcList()){
                HellProto.NpcTemp.Builder npcTempMsg = HellProto.NpcTemp.newBuilder();
                npcTempMsg.setNpcId(npc.getNpcId());
                npcTempMsg.setNo(npc.getNo());
                npcTempMsg.setAgeId(npc.getAgeId());
                npcTempMsg.setQualityId(npc.getQualityId());
                for(List<String> eventInfoList : npc.getEventMap().values()){
                    for(String eventInfoStr : eventInfoList){
                        List<Integer> tempList = StringUtils.stringToIntegerList(eventInfoStr,"=");
                        HellProto.EventTemp.Builder eventTempMsg = HellProto.EventTemp.newBuilder();
                        eventTempMsg.setEventId(tempList.get(0));
                        eventTempMsg.setAge(tempList.get(1));
                        npcTempMsg.addEventList(eventTempMsg);
                    }
                }
                respMsg.addNpcList(npcTempMsg);
            }
        }
        player.sendPacket(Protocol.U_HELL_CUT_LAW, respMsg);
    }
}
